{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9.15 \u5b9a\u4e49\u6709\u53ef\u9009\u53c2\u6570\u7684\u5143\u7c7b\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5b9a\u4e49\u4e00\u4e2a\u5143\u7c7b\uff0c\u5141\u8bb8\u7c7b\u5b9a\u4e49\u65f6\u63d0\u4f9b\u53ef\u9009\u53c2\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u63a7\u5236\u6216\u914d\u7f6e\u7c7b\u578b\u7684\u521b\u5efa\u8fc7\u7a0b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5b9a\u4e49\u7c7b\u7684\u65f6\u5019\uff0cPython\u5141\u8bb8\u6211\u4eec\u4f7f\u7528 ``metaclass``\u5173\u952e\u5b57\u53c2\u6570\u6765\u6307\u5b9a\u7279\u5b9a\u7684\u5143\u7c7b\u3002\n\u4f8b\u5982\u4f7f\u7528\u62bd\u8c61\u57fa\u7c7b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from abc import ABCMeta, abstractmethod\nclass IStream(metaclass=ABCMeta):\n    @abstractmethod\n    def read(self, maxsize=None):\n        pass\n\n    @abstractmethod\n    def write(self, data):\n        pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u800c\uff0c\u5728\u81ea\u5b9a\u4e49\u5143\u7c7b\u4e2d\u6211\u4eec\u8fd8\u53ef\u4ee5\u63d0\u4f9b\u5176\u4ed6\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Spam(metaclass=MyMeta, debug=True, synchronize=True):\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u4f7f\u5143\u7c7b\u652f\u6301\u8fd9\u4e9b\u5173\u952e\u5b57\u53c2\u6570\uff0c\u4f60\u5fc5\u987b\u786e\u4fdd\u5728 __prepare__() , __new__() \u548c __init__() \u65b9\u6cd5\u4e2d\n\u90fd\u4f7f\u7528\u5f3a\u5236\u5173\u952e\u5b57\u53c2\u6570\u3002\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class MyMeta(type):\n    # Optional\n    @classmethod\n    def __prepare__(cls, name, bases, *, debug=False, synchronize=False):\n        # Custom processing\n        pass\n        return super().__prepare__(name, bases)\n\n    # Required\n    def __new__(cls, name, bases, ns, *, debug=False, synchronize=False):\n        # Custom processing\n        pass\n        return super().__new__(cls, name, bases, ns)\n\n    # Required\n    def __init__(self, name, bases, ns, *, debug=False, synchronize=False):\n        # Custom processing\n        pass\n        super().__init__(name, bases, ns)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed9\u4e00\u4e2a\u5143\u7c7b\u6dfb\u52a0\u53ef\u9009\u5173\u952e\u5b57\u53c2\u6570\u9700\u8981\u4f60\u5b8c\u5168\u5f04\u61c2\u7c7b\u521b\u5efa\u7684\u6240\u6709\u6b65\u9aa4\uff0c\n\u56e0\u4e3a\u8fd9\u4e9b\u53c2\u6570\u4f1a\u88ab\u4f20\u9012\u7ed9\u6bcf\u4e00\u4e2a\u76f8\u5173\u7684\u65b9\u6cd5\u3002\n__prepare__() \u65b9\u6cd5\u5728\u6240\u6709\u7c7b\u5b9a\u4e49\u5f00\u59cb\u6267\u884c\u524d\u9996\u5148\u88ab\u8c03\u7528\uff0c\u7528\u6765\u521b\u5efa\u7c7b\u547d\u540d\u7a7a\u95f4\u3002\n\u901a\u5e38\u6765\u8bb2\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u53ea\u662f\u7b80\u5355\u7684\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\u6216\u5176\u4ed6\u6620\u5c04\u5bf9\u8c61\u3002\n__new__() \u65b9\u6cd5\u88ab\u7528\u6765\u5b9e\u4f8b\u5316\u6700\u7ec8\u7684\u7c7b\u5bf9\u8c61\u3002\u5b83\u5728\u7c7b\u7684\u4e3b\u4f53\u88ab\u6267\u884c\u5b8c\u540e\u5f00\u59cb\u6267\u884c\u3002\n__init__() \u65b9\u6cd5\u6700\u540e\u88ab\u8c03\u7528\uff0c\u7528\u6765\u6267\u884c\u5176\u4ed6\u7684\u4e00\u4e9b\u521d\u59cb\u5316\u5de5\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u6211\u4eec\u6784\u9020\u5143\u7c7b\u7684\u65f6\u5019\uff0c\u901a\u5e38\u53ea\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a __new__() \u6216 __init__() \u65b9\u6cd5\uff0c\u4f46\u4e0d\u662f\u4e24\u4e2a\u90fd\u5b9a\u4e49\u3002\n\u4f46\u662f\uff0c\u5982\u679c\u9700\u8981\u63a5\u53d7\u5176\u4ed6\u7684\u5173\u952e\u5b57\u53c2\u6570\u7684\u8bdd\uff0c\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5c31\u8981\u540c\u65f6\u63d0\u4f9b\uff0c\u5e76\u4e14\u90fd\u8981\u63d0\u4f9b\u5bf9\u5e94\u7684\u53c2\u6570\u7b7e\u540d\u3002\n\u9ed8\u8ba4\u7684 __prepare__() \u65b9\u6cd5\u63a5\u53d7\u4efb\u610f\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u4f46\u662f\u4f1a\u5ffd\u7565\u5b83\u4eec\uff0c\n\u6240\u4ee5\u53ea\u6709\u5f53\u8fd9\u4e9b\u989d\u5916\u7684\u53c2\u6570\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u7c7b\u547d\u540d\u7a7a\u95f4\u7684\u521b\u5efa\u65f6\u4f60\u624d\u9700\u8981\u53bb\u5b9a\u4e49 __prepare__() \u65b9\u6cd5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u4f7f\u7528\u5f3a\u5236\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5728\u7c7b\u7684\u521b\u5efa\u8fc7\u7a0b\u4e2d\u6211\u4eec\u5fc5\u987b\u901a\u8fc7\u5173\u952e\u5b57\u6765\u6307\u5b9a\u8fd9\u4e9b\u53c2\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u914d\u7f6e\u4e00\u4e2a\u5143\u7c7b\u8fd8\u53ef\u4ee5\u89c6\u4f5c\u5bf9\u7c7b\u53d8\u91cf\u7684\u4e00\u79cd\u66ff\u4ee3\u65b9\u5f0f\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Spam(metaclass=MyMeta):\n    debug = True\n    synchronize = True\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c06\u8fd9\u4e9b\u5c5e\u6027\u5b9a\u4e49\u4e3a\u53c2\u6570\u7684\u597d\u5904\u5728\u4e8e\u5b83\u4eec\u4e0d\u4f1a\u6c61\u67d3\u7c7b\u7684\u540d\u79f0\u7a7a\u95f4\uff0c\n\u8fd9\u4e9b\u5c5e\u6027\u4ec5\u4ec5\u53ea\u4ece\u5c5e\u4e8e\u7c7b\u7684\u521b\u5efa\u9636\u6bb5\uff0c\u800c\u4e0d\u662f\u7c7b\u4e2d\u7684\u8bed\u53e5\u6267\u884c\u9636\u6bb5\u3002\n\u53e6\u5916\uff0c\u5b83\u4eec\u5728 __prepare__() \u65b9\u6cd5\u4e2d\u662f\u53ef\u4ee5\u88ab\u8bbf\u95ee\u7684\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u5728\u6240\u6709\u7c7b\u4e3b\u4f53\u6267\u884c\u524d\u88ab\u6267\u884c\u3002\n\u4f46\u662f\u7c7b\u53d8\u91cf\u53ea\u80fd\u5728\u5143\u7c7b\u7684 __new__() \u548c __init__() \u65b9\u6cd5\u4e2d\u53ef\u89c1\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}